﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.ComponentModel.Design;
using System.Reflection;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;
using System.Text;


namespace Tesla.Util.UIUtilities
{
    
    /// <summary>
    /// Generic DHTML Menu Web Custom Control
    /// </summary>
    [DefaultProperty("Text"),
    Designer(typeof(MenuCtrlDesigner)), ToolboxData("<{0}:TeslaMenu runat=server></{0}:TeslaMenu>")]
    public class TeslaMenu : System.Web.UI.WebControls.WebControl
    {
        #region Private Variables
        private string stylesheet;
        private string scriptname;
        private string xmlfilename;
        private string xslfilename;
        #endregion

        #region Properties

        [Bindable(true),
        Category("Appearance"),
        DefaultValue("menu.css")]
        public string StylesheetName
        {
            get
            {
                return stylesheet;
            }

            set
            {
                stylesheet=value;
            }
        }

        [Bindable(true),
        Category("Appearance"),
        DefaultValue("menu.js")]
        public string ScriptName
        {
            get
            {
                return scriptname;
            }

            set
            {
                scriptname=value;
            }
        }

        [Bindable(true),
        Category("Appearance"),
        DefaultValue("menu.xml")]
        public string XmlFileName
        {
            get
            {
                return xmlfilename;
            }

            set
            {
                xmlfilename=value;
            }
        }

        [Bindable(true),
        Category("Appearance"),
        DefaultValue("menu.xsl")]
        public string XslFileName
        {
            get
            {
                return xslfilename;
            }

            set
            {
                xslfilename=value;
            }
        }
        #endregion


        #region Constructor
        public TeslaMenu()
        {
            if(XmlFileName==null) XmlFileName="menu.xml";
            if(XslFileName==null) XslFileName="menu.xsl";
            if(ScriptName==null) ScriptName="menu.js";
            if(StylesheetName==null) StylesheetName="menu.css";
        }
        #endregion


        #region Custom Methods
        /// <summary>
        /// Render the control
        /// </summary>
        /// <param name="output"> The HTML writer to write out to </param>
        protected override void Render(HtmlTextWriter output)
        {
            if (XmlFileName.Length <5) XmlFileName="menu.xml";
            if(XslFileName.Length <5) XslFileName="menu.xsl";
            if(ScriptName.Length <4) ScriptName="menu.js";
            if(StylesheetName.Length <5) StylesheetName="menu.css";
                    
            string XmlSystemFileName = System.Web.HttpContext.Current.Server.MapPath(XmlFileName); 
            string XslSystemFileName = System.Web.HttpContext.Current.Server.MapPath(XslFileName); 
            string ScriptSystemFileName = System.Web.HttpContext.Current.Server.MapPath(ScriptName); 
            string StylesheetSystemFileName = System.Web.HttpContext.Current.Server.MapPath(StylesheetName); 
            string strContent="<link rel=\"stylesheet\" type=\"text/css\" href=\"" + StylesheetSystemFileName + "\">";
            strContent+="<script language=\"javascript\" src=\"" + ScriptSystemFileName +"\"></script> ";


            XslTransform xslt = new XslTransform();
            xslt.Load(XslSystemFileName);
            XPathDocument xpathdocument = new
            XPathDocument(XmlSystemFileName);            
            StringBuilder sb = new StringBuilder(); 
            StringWriter sw = new StringWriter(sb); 
            xslt.Transform(xpathdocument, null, sw, null);
            strContent+= sb.ToString(); 
            output.Write(strContent);
        }
        #endregion
    }

    public class MenuCtrlDesigner : ControlDesigner
    {
        #region Private Members
        private TeslaMenu menuctrl;
        #endregion

        #region Constructor
        public MenuCtrlDesigner()
        {
        }
        #endregion

        #region Custom Overriden Methods
        public override string GetDesignTimeHtml()
        {
            return "<div id=\'div1\' style=\'align: center; COLOR:Blue; valign: middle; background-color:FFCC66;border-width:2px;\' >Menu Table Here</div>";
        }

        public override void Initialize(IComponent component)
        {
            menuctrl = (TeslaMenu)component;
            base.Initialize(component);
            return;
        }
        #endregion
    }
}


    
